package net.wanho.service.impl;

import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import net.wanho.constant.CaptchaConstant;
import net.wanho.service.CaptchaService;
import net.wanho.vo.CaptchaVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * Author：汤小洋
 * Date：2024-10-18 16:25
 * Description：<描述>
 */
@Service
public class CaptchaServiceImpl implements CaptchaService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public CaptchaVo captcha() {
        // 生成验证码
        Captcha captcha = new SpecCaptcha();

        // 生成图片的base64格式
        String base64 = captcha.toBase64();
        // 生成uuid
        String uuid = UUID.randomUUID().toString();

        // 将验证码保存到redis中
        redisTemplate.opsForValue().set(CaptchaConstant.CAPTCHA_PREFIX + uuid, captcha.text(), CaptchaConstant.CAPTCHA_EXPIRE, TimeUnit.MINUTES);


        return new CaptchaVo(uuid, base64);
    }
}
